<template>
	<view class="content">
		<image class="topbg" mode="scaleToFill" src="../../static/img/mine_top_bg.png"></image>
		<view class="userinfo-view">
			<view class="touxiang-name-view">
				<image class="touxiang-img" mode="scaleToFill" :src="headUrl" @error="imageError"></image>

				<text v-if="JSON.stringify(userinfo) == '{}'" class="login-btn" @click="tologin">点击登录</text>
				<view v-else class="mobile-view">
					<text class="nickname-text">{{userinfo.nickname}}</text>
					<text class="mobile-text">{{userinfo.username}}</text>
				</view>

			</view>
		</view>

		<view class="center-view">
			<view class="top-heng" @click="startCollection">
				<image class="top-btn-img" mode="scaleToFill" src="../../static/img/mine_btn_shoucang.png"></image>
				<text class="top-btn-text">收藏</text>
			</view>
			<view class="top-heng" @click="startComment">
				<image class="top-btn-img" mode="scaleToFill" src="../../static/img/mine_btn_pinglun.png"></image>
				<text class="top-btn-text">评论</text>
			</view>
			<view class="top-heng" @click="startMessage">
				<image class="top-btn-img" mode="scaleToFill" src="../../static/img/mine_btn_xiaoxi.png"></image>
				<text class="top-btn-text">消息</text>
			</view>

		</view>


		<view class="bottom-view">
			<u-cell-group class="bottom-view2" :border="false">
				<u-cell-item icon="account" :title-style="itemTitleStyle" @click="startUserinfo" title="个人中心">
				</u-cell-item>
				<u-cell-item icon="setting" :title-style="itemTitleStyle" @click="startSafetyset" title="安全设置">
				</u-cell-item>
				<u-cell-item icon="edit-pen" :title-style="itemTitleStyle" @click="startFeedback" title="意见反馈">
				</u-cell-item>
				<u-cell-item icon="info-circle" :title-style="itemTitleStyle" @click="startAbout" title="关于我们"
					:border-bottom="false">
				</u-cell-item>
			</u-cell-group>
		</view>

		<view v-if="JSON.stringify(userinfo) != '{}'" class="login-out" @click="loginOut">退出登录</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				userinfo: {},
				itemTitleStyle: {
					'font-size': '14px',
					'line-height': '75rpx'
				}
			}
		},
		computed: {
			headUrl: function() {
				let url = '';
				if (!this.userinfo || !this.userinfo.headicon) {
					url = '../../static/img/ic_touxiang.png'
				} else {
					url = this.$mConfig.formatFileUrl(this.userinfo.headicon);
				}
				return url;
			}
		},
		onLoad() {

		},
		onShow() {
			this.requestUserinfo();
		},
		methods: {
			tologin() {
				uni.navigateTo({
					url: "../login/login"
				})
			},
			startUserinfo() {
				if (!this.isLogin()) {
					return;
				}
				uni.navigateTo({
					url: "../userinfo/userinfo"
				})
			},
			startAbout() {
				uni.navigateTo({
					url: "../about/about"
				})
			},
			startFeedback() {
				if (!this.isLogin()) {
					return;
				}
				uni.navigateTo({
					url: "../feedback/feedback"
				})
			},
			startSafetyset() {
				if (!this.isLogin()) {
					return;
				}
				uni.navigateTo({
					url: "../safetyset/safetyset"
				})
			},
			startCollection() {
				if (!this.isLogin()) {
					return;
				}
				uni.navigateTo({
					url: "../minecollection/minecollection"
				})
			},
			startComment() {
				if (!this.isLogin()) {
					return;
				}
				uni.navigateTo({
					url: "../minecomment/minecomment"
				})
			},
			startMessage() {
				if (!this.isLogin()) {
					return;
				}
				uni.navigateTo({
					url: "../minemessage/minemessage"
				})
			},
			requestUserinfo() {

				if (uni.getStorageSync('token').length == 0) {
					return
				}

				this.$u.post('/client/user/userinfo', {}).then(res => {
					if (res.code == 200) {
						this.userinfo = res.user
						uni.setStorageSync('userinfo', this.userinfo);
					}
				});
			},
			loginOut() {
				uni.showModal({
					// title: '提示',
					content: '是否退出登录？',
					success: (res) => {
						if (res.confirm) {
							this.userinfo = {}
							uni.setStorageSync('token', '');
							// console.log('用户点击确定');
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
					}
				});
			},
			isLogin() {
				if (JSON.stringify(this.userinfo) == '{}') {
					uni.showToast({
						title: "未登录",
						duration: 2000,
						icon: 'none',
						position: 'bottom'
					});
					return false;
				} else {
					return true;
				}
			},
			imageError: function(e) {
				console.error('image发生error事件，携带值为' + e.detail.errMsg)
			}

		}
	}
</script>

<style lang="scss">
	page {
		background-color: $uni-bg-color-grey;
	}

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
	}


	.topbg {
		position: absolute;
		width: 100%;
		height: 400rpx;
		z-index: 0;
	}

	.userinfo-view {
		display: flex;
		flex-direction: column;
		width: 90%;
		margin-top: 110rpx;
		z-index: 100;

		.touxiang-name-view {
			display: flex;
			width: 100%;
			flex-direction: row;

			.touxiang-img {
				border: $uni-bg-color solid 4rpx;
				width: 130rpx;
				border-radius: 75rpx;
				height: 130rpx;
			}

			.login-btn {
				color: $uni-bg-color;
				margin-left: 30rpx;
				font-size: 19px;
				line-height: 130rpx;
			}

			.mobile-view {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				margin-left: 30rpx;
				padding: 10rpx 0 10rpx 0;
				height: 130rpx;

				.nickname-text {
					color: $uni-bg-color;
					font-size: 19px;
				}

				.mobile-text {
					color: $uni-bg-color;
					font-size: 15px;
				}
			}
		}
	}


	.center-view {
		display: flex;
		flex-direction: row;
		margin-top: 60rpx;
		z-index: 100;
		padding-left: 110rpx;
		padding-right: 110rpx;
		height: 170rpx;
		justify-content: space-between;
		align-items: center;
		border-radius: 8rpx;
		background-color: $uni-bg-color;
		width: 90%;


		.top-heng {
			display: flex;
			height: 160rpx;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			.top-btn-img {
				width: 45rpx;
				height: 45rpx;
				will-change: transform;
			}

			.top-btn-text {
				margin-top: 12rpx;
				font-size: 11px;
				text-align: center;
				color: $uni-text-color;
			}
		}
	}

	.bottom-view {
		width: 90%;
		margin-top: 25rpx;
		z-index: 100;
		margin-bottom: 25rpx;
		border-radius: 10rpx;
		background-color: $uni-bg-color;

		.bottom-view2 {
			width: 100%;
			padding: 0rpx 8rpx 0rpx 8rpx;
		}
	}

	.login-out {
		margin: 10rpx 0 0 0;
		font-size: 15px;
		width: 90%;
		border-radius: 9rpx;
		line-height: 100rpx;
		text-align: center;
		color: #999;
		background-color: #fff;
	}
</style>
